package cn.le.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by admin on 2017/7/26.
 */
public class EvenChecker implements Runnable {

    private IntGennerator gennerator;
    private final int id;

    public EvenChecker(IntGennerator g,int ident)
    {
        gennerator = g;
        id = ident;
    }
    public void run()
    {
        System.out.println("run begin");
        while (!gennerator.isCanceled()) {
            int val = gennerator.next();
            if (val % 2 != 0) {
                System.out.println(val + " not even!");
                gennerator.cancel();
            }
        }
        System.out.println("run end");
    }
    public static void test(IntGennerator g,int count)
    {
        System.out.println("Press Control-C to exit");
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < count; i++)
        {
            exec.execute(new EvenChecker(g,i));
        }
    }
}
